package servlet;

import bean.Result;
import biz.BizException;
import biz.userBiz;
import com.google.gson.Gson;
import dao.QueryUserdao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@WebServlet("/address.s")
public class AddAddressServlet extends HttpServlet {

    userBiz biz =new userBiz();
    QueryUserdao dao=new QueryUserdao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final String username = req.getParameter("username");
        final String province = req.getParameter("province");
        final String city = req.getParameter("city");
        final String site = req.getParameter("site");
        String address=province+city+site;
        final String telphone = req.getParameter("telphone");
        final HttpSession session = req.getSession();
        final Object name = session.getAttribute("user");
        final Map<String, Object> map = dao.queryid((String) name);
        final Integer uid = (Integer) map.get("id");
        Result result=null;
        System.out.println(address);
        System.out.println(uid);
        try {
            biz.add_address(username,telphone,uid,address);
            result =new Result(1,"添加成功",null);
        } catch (BizException e) {
            result =new Result(0,e.getMessage(),null);
        }
        catch (Exception e1){
            result =new Result(0,e1.getMessage(),null);
        }
        final String json = new Gson().toJson(result);
        resp.getWriter().append(json);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}
